<template>
    <div>
      <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
            <!-- 搜索框-->
         <!-- <el-form-item label="员工编号" prop="workNumber">
               <el-input size="small" v-model="searchForm.workNumber" placeholder="员工编号" clearable></el-input>
         </el-form-item> -->
         <el-form-item label="姓名" prop="name">
                <el-input size="small" v-model="searchForm.name" placeholder="姓名" clearable></el-input>
         </el-form-item>
         <el-form-item label="单位" prop="unit">
            <el-select v-model="searchForm.unit" filterable multiple placeholder="请填写单位" style="width:100%;" >
              <el-option v-for="item in unitArr" :label="item.organization" :value="item.organization" :key="item.id"></el-option>
            </el-select>
         </el-form-item>
         <el-form-item label="职务" prop="currentPosition">
                <el-input size="small" v-model="searchForm.currentPosition" placeholder="职务" clearable></el-input>
         </el-form-item>
         <el-form-item label="岗位类别" prop="identity">
              <el-select v-model="searchForm.identity" filterable multiple placeholder="请填写岗位类别" style="width:100%;" >
                <el-option v-for="item in identityArr" :label="item.identity" :value="item.identity" :key="item.id"></el-option>
              </el-select>
         </el-form-item>
         <el-form-item label="票类别" prop="ticket">
              <el-select v-model="searchForm.ticket" filterable multiple placeholder="请填写票类别" style="width:100%;" >
                <el-option v-for="item in tickets" :label="item" :value="item" :key="item"></el-option>
              </el-select>
         </el-form-item>
         <!-- <el-form-item label="照片" prop="photo">
                <el-input size="small" v-model="searchForm.photo" placeholder="照片" clearable></el-input>
         </el-form-item> -->
         <!-- <el-form-item label="备注信息" prop="remarks">
                <el-input size="small" v-model="searchForm.remarks" placeholder="备注信息" clearable></el-input>
         </el-form-item> -->
         <el-form-item label="年" prop="createdate">
           <el-input-number v-model="createdate" controls-position="right" :min="2021" :max="9999"></el-input-number>
         </el-form-item>
         <el-form-item label="投票次数" prop="ticketCount">
           <el-input-number v-model="ticketCount" controls-position="right" :min="1" :max="9999"></el-input-number>
         </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="refreshList()" size="small">查询</el-button>
            <el-button @click="resetSearch()" size="small">重置</el-button>
          </el-form-item>
      </el-form>
        <!-- 导入导出-->
      <el-dialog  title="导入Excel" :visible.sync="isImportCollapse">
          <el-form size="small" :inline="true" v-show="isImportCollapse"  ref="importForm">
             <el-form-item>
              <el-button type="default" @click="downloadTpl()" size="small">下载模板</el-button>
             </el-form-item>
             <el-form-item prop="loginName">
                <el-upload
                  class="upload-demo"
                  :action="`${this.$http.BASE_URL}/peopleinformation/peopleassessdata/peopleAssessInformation/import`"
                  :on-success="uploadSuccess"
                   :show-file-list="true">
                  <el-button size="small" type="primary">点击上传</el-button>
                  <div slot="tip" class="el-upload__tip">只允许导入“xls”或“xlsx”格式文件！</div>
                </el-upload>
            </el-form-item>
          </el-form>
      </el-dialog>
      <div class="bg-white top">
      <el-row>
        <!-- <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:add')" type="primary" size="small" icon="el-icon-plus" @click="add()">新建</el-button> -->
        <!-- <el-select size="small" v-model="ticket" placeholder="请填写票类别" >
          <el-option v-for="item in ticketse" :label="item" :value="item" :key="item"></el-option>
        </el-select>
        <el-button type="warning" size="small" icon="el-icon-edit-outline" @click="createAccounts()" plain>生成票模板</el-button> -->
        <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:del')" type="danger"   size="small" icon="el-icon-delete" @click="del()"
                  :disabled="dataListSelections.length <= 0" plain>删除
        </el-button>
        <el-button-group class="pull-right">
            <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:import')" type="default" size="small" icon="el-icon-upload2" title="导入" @click="isImportCollapse = !isImportCollapse"></el-button>
            <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:export')" type="default" size="small" icon="el-icon-download" title="导出" @click="exportExcel()"></el-button>
            <el-button
              type="default"
              size="small"
              icon="el-icon-refresh"
              @click="refreshList">
            </el-button>
        </el-button-group>
      </el-row>
    <el-table
      :data="dataList"
       size="small"
      @selection-change="selectionChangeHandle"
      @sort-change="sortChangeHandle"
      v-loading="loading"
      class="table">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
      label="序号"
      type="index">
      </el-table-column>
    <!-- <el-table-column
        prop="workNumber"
        show-overflow-tooltip
        sortable="custom"
        label="员工编号">
      </el-table-column> -->
    <el-table-column
        prop="name"
        show-overflow-tooltip
        label="姓名">
      </el-table-column>
    <el-table-column
        prop="unit"
        show-overflow-tooltip
        label="单位">
      </el-table-column>
    <el-table-column
        prop="currentPosition"
        show-overflow-tooltip
        label="职务">
      </el-table-column>
    <el-table-column
        prop="identity"
        show-overflow-tooltip
        label="岗位类别">
      </el-table-column>
      <el-table-column
          prop="ticket"
          show-overflow-tooltip
          label="票类别">
        </el-table-column>
    <!-- <el-table-column
        prop="photo"
        show-overflow-tooltip
        sortable="custom"
        label="照片">
        <template slot-scope="scope" v-if="scope.row.photo">
        <el-image
          style="height: 50px;width:50px;margin-right:10px;"
          :src="src" v-for="(src, index) in scope.row.photo.split('|')" :key="index"
          :preview-src-list="scope.row.photo.split('|')">
        </el-image>
        </template>
      </el-table-column> -->
    <!-- <el-table-column
        prop="remarks"
        show-overflow-tooltip
        sortable="custom"
        label="备注信息">
      </el-table-column> -->
      <el-table-column
        header-align="center"
        align="center"
        fixed="right"
        :key="Math.random()"
        width="200"
        label="操作">
        <template  slot-scope="scope">
          <!-- <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:view')" type="text" icon="el-icon-view" size="small" @click="view(scope.row.id)">查看</el-button> -->
          <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:edit')" type="text" icon="el-icon-edit" size="small" @click="edit(scope.row.id)">修改</el-button>
          <el-button v-if="hasPermission('peopleinformation:peopleassessdata:peopleAssessInformation:del')" type="text"  icon="el-icon-delete" size="small" @click="del(scope.row.userId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageNo"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="total"
      background
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    </div>
        <!-- 弹窗, 新增 / 修改 -->
    <PeopleAssessInformationForm  ref="peopleAssessInformationForm" @refreshDataList="refreshList"></PeopleAssessInformationForm>
    <asyn-for-tool ref='asynForTool' @returnData='returnData(arguments)'></asyn-for-tool>
  </div>
</template>

<script>
  import PeopleAssessInformationForm from './PeopleAssessInformationForm'
  export default {
    data () {
      return {
        searchForm: {
          name: '',
          unit: '',
          currentPosition: '',
          identity: '',
          photo: '',
          workNumber: '',
          ticket: '',
          ticketCount: ''
          // remarks: '',
        },
        createdate: new Date().getFullYear(),
        ticketCount: 1,
        finishNumber: 0,
        submitNumber: 0,
        loadings: '',
        tickets: [
        '所有中层干部(校领导票)',
        '教辅中层干部(服务对象票)',
        '教学教辅中层干部(同级票)',
        '机关中层干部(服务对象票)',
        '机关中层干部(同级票)',
        '中层干部(群众票)'
        ],
        ticketse: [
        '所有中层干部(校领导票)',
        '教辅中层干部(服务对象票)',
        '教学教辅中层干部(同级票)',
        '机关中层干部(服务对象票)',
        '机关中层干部(同级票)',
        '中层干部(群众票)'
        ],
        deleteArr: [],
        deleteAllArr: [],
        searchList: [],
        state: '',
        dataListSelectionDealAgain: [],
        ticketsData: [],
        unitArr: [],
        ticket: '',
        dataList: [],
        identityArr: [],
        pageNo: 1,
        pageSize: 10,
        total: 0,
        orderBy: '',
        dataListSelections: [],
        isImportCollapse: false,
        loading: false
      }
    },
    components: {
      PeopleAssessInformationForm
    },
    activated () {
      this.refreshList()
    },
    methods: {
      // importAssess () {
      //     this.$confirm(`您导入数据数量为${this.dataListSelections.length}实际可导入数据数量为${this.dataListSelectionDealAgain.length}点击确认后开始导入如有问题请核对是否重复或者等待时间过短点击取消即可!`, '统计完成', {
      //         confirmButtonText: '确定',
      //         cancelButtonText: '取消',
      //         showClose: false,
      //         type: 'warning'
      //       }).then(() => {
      //         if (this.dataListSelectionDealAgain.length !== 0) {
      //           this.state = 'save'
      //           this.loadFnc('保存中，请勿刷新网页')
      //           this.finishNumber = 0
      //           this.submitNumber = this.dataListSelections.length
      //           this.$refs.asynForTool.func('/peopleinformation/peopleassessdata/peopleAssessInformation/save', 'post', this.dataListSelections, true)
      //           if (this.ticket === '所有中层干部(校领导票)') {
      //             this.$refs.asynForTool.func('/ticket/schleamiddlecadres/schoolleaderMiddlecadres/save', 'post', this.dataListSelectionDealAgain)
      //           } else if (this.ticket === '教学教辅中层干部(同级票)') {
      //             this.$refs.asynForTool.func('/ticket/samelevelteach/samelevelTeach/save', 'post', this.dataListSelectionDealAgain)
      //           } else if (this.ticket === '机关中层干部(同级票)') {
      //             this.$refs.asynForTool.func('/ticket/samelevelorgan/samelevelOrgan/save', 'post', this.dataListSelectionDealAgain)
      //           } else if (this.ticket === '教辅中层干部(服务对象票)') {
      //             this.$refs.asynForTool.func('/ticket/serviceteach/serviceTeach/save', 'post', this.dataListSelectionDealAgain)
      //           } else if (this.ticket === '机关中层干部(服务对象票)') {
      //             this.$refs.asynForTool.func('/ticket/serviceorgan/serviceOrgan/save', 'post', this.dataListSelectionDealAgain)
      //           } else if (this.ticket === '中层干部(群众票)') {
      //             this.$refs.asynForTool.func('/ticket/crowdmiddleleader/crowdMiddleleader/save', 'post', this.dataListSelectionDealAgain)
      //           }
      //         }
      //       }).catch(() => {
      //         this.$message({
      //           type: 'info',
      //           message: '导入取消'
      //         })
      //       })
      // },
      loadFnc (content) {
        this.loadings = this.$loading({
          lock: true,
          text: content,
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.searchForm.ticketCount = ''
        if (this.createdate === '' || this.createdate === null || this.createdate === undefined || this.ticketCount === undefined || this.ticketCount === undefined || this.ticketCount === undefined) {
          this.searchForm.ticketCount = new Date().getFullYear() + '-' + 1
        } else {
          this.searchForm.ticketCount = this.createdate + '-' + this.ticketCount
        }
        if (this.searchForm.unit !== '') {
          this.searchForm.unit = this.searchForm.unit.join(',')
        }
        if (this.searchForm.identity !== '') {
          this.searchForm.identity = this.searchForm.identity.join(',')
        }
        if (this.searchForm.ticket !== '') {
          this.searchForm.ticket = this.searchForm.ticket.join(',')
        }
        this.$http({
          url: '/peopleinformation/peopleassessdata/peopleAssessInformation/list',
          method: 'get',
          params: {
            'pageNo': this.pageNo,
            'pageSize': this.pageSize,
            'orderBy': this.orderBy,
            ...this.searchForm
          }
        }).then(({data}) => {
          if (data && data.success) {
            if (this.searchForm.unit !== '') {
              this.searchForm.unit = this.searchForm.unit.split(',')
            }
            if (this.searchForm.identity !== '') {
              this.searchForm.identity = this.searchForm.identity.split(',')
            }
            if (this.searchForm.ticket !== '') {
              this.searchForm.ticket = this.searchForm.ticket.split(',')
            }
            this.dataList = data.page.list
            this.total = data.page.count
            this.loading = false
            this.searchIdentity()
            this.searchOneInstitutional()
          }
        })
      },
      // 搜索框下拉数据提供 单位名称下拉框数据请求
      searchOneInstitutional () {
        this.$http({
          url: '/peopleinformation/organization/nameOfOrganization/treeData',
          method: 'get'
        }).then(({data}) => {
          this.unitArr = []
          for (let index = 0; index < data.treeData.length; index++) {
            this.unitArr.push(data.treeData[index])
          }
        })
      },
      // 搜索框下拉数据提供 岗位类别下拉框数据请求
      searchIdentity () {
        this.$http({
          url: '/peopleinformation/identityiformation/peopleIdentityInformation/list',
          method: 'get',
          params: {
            identity: ''
          }
        }).then(({data}) => {
          this.identityArr = data.page.list
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageNo = 1
        this.refreshList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageNo = val
        this.refreshList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },

    // 排序
      sortChangeHandle (obj) {
        if (obj.order === 'ascending') {
          this.orderBy = obj.prop + ' asc'
        } else if (obj.order === 'descending') {
          this.orderBy = obj.prop + ' desc'
        } else {
          this.orderBy = ''
        }
        this.refreshList()
      },
      // 新增
      add () {
        this.$refs.peopleAssessInformationForm.init('add', '')
      },
      // 修改
      edit (id) {
        id = id || this.dataListSelections.map(item => {
          return item.id
        })[0]
        this.$refs.peopleAssessInformationForm.init('edit', id)
      },
      // 查看
      view (id) {
        this.$refs.peopleAssessInformationForm.init('view', id)
      },
      // 删除
      del (userId) {
        let ids = userId || this.dataListSelections.map(item => {
          return item.userId
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.$http({
            url: '/peopleinformation/peopleassessdata/peopleAssessInformation/delete',
            method: 'delete',
            params: {
              'ids': ids,
              'ticketCount': this.searchForm.ticketCount
              }
          }).then(({data}) => {
            if (data && data.success) {
              this.$http({
                url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteSamelevelTeach',
                method: 'delete',
                params: {
                  'ids': ids,
                  'ticketCount': this.searchForm.ticketCount
                  }
              }).then(({data}) => {
                if (data && data.success) {
                  this.$http({
                    url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteSamelevelOrgan',
                    method: 'delete',
                    params: {
                      'ids': ids,
                      'ticketCount': this.searchForm.ticketCount
                      }
                  }).then(({data}) => {
                    if (data && data.success) {
                      this.$http({
                        url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteServiceTeach',
                        method: 'delete',
                        params: {
                          'ids': ids,
                          'ticketCount': this.searchForm.ticketCount
                          }
                      }).then(({data}) => {
                        if (data && data.success) {
                          this.$http({
                            url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteServiceOrgan',
                            method: 'delete',
                            params: {
                              'ids': ids,
                              'ticketCount': this.searchForm.ticketCount
                              }
                          }).then(({data}) => {
                            if (data && data.success) {
                              this.$http({
                                url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteCrowdMiddleleader',
                                method: 'delete',
                                params: {
                                  'ids': ids,
                                  'ticketCount': this.searchForm.ticketCount
                                  }
                              }).then(({data}) => {
                                if (data && data.success) {
                                  this.$http({
                                    url: '/peopleinformation/peopleassessdata/peopleAssessInformation/deleteSchoolleaderMiddlecadres',
                                    method: 'delete',
                                    params: {
                                      'ids': ids,
                                      'ticketCount': this.searchForm.ticketCount
                                      }
                                  }).then(({data}) => {
                                    if (data && data.success) {
                                      this.$http({
                                        url: '/peopleinformation/peopleassessdata/peopleAssessInformation/list',
                                        method: 'get',
                                        params: {
                                          'pageSize': 9999,
                                          'ticketCount': this.searchForm.ticketCount
                                        }
                                      }).then(({data}) => {
                                        if (data.page.list.length === 0) {
                                          this.$http({
                                            url: '/peopleinformation/checktime/checkTime/deleteCheck',
                                            method: 'delete',
                                            params: {
                                              'ids': this.searchForm.ticketCount
                                            }
                                          })
                                        }
                                        this.refreshList()
                                        this.loading = false
                                      })
                                    }
                                    })
                                }
                                })
                            }
                            })
                        }
                        })
                    }
                    })
                }
              })
            }
          })
        })
      },
      returnData (value) {
        this.finishNumber++
        if (this.state === 'save') {
          this.finishNumber++
          if (this.finishNumber === this.submitNumber) {
            this.refreshList()
            this.loadings.close()
          }
        } else if (this.state === 'list') {
          // 做不重复添加数据处理
          if (value[0].page.list.length === 0) {
            this.dataListSelectionDealAgain.push(this.ticketsData[value[2]])
          }
          if (this.finishNumber === this.submitNumber) {
            this.loadings.close()
            this.importAssess()
          }
        }
      },
      // 生成票模板
      // createAccounts () {
      //   this.ticketsData = []
      //   this.searchList = []
      //   this.loadFnc('查找中，请勿刷新网页')
      //   for (let index in this.dataListSelections) {
      //     this.ticketsData.push({})
      //     this.searchList.push({})
      //     this.$set(this.searchList[index], 'mark', this.dataListSelections[index].userId)
      //     this.$set(this.ticketsData[index], 'name', this.dataListSelections[index].name)
      //     this.$set(this.ticketsData[index], 'currentPosition', this.dataListSelections[index].currentPosition)
      //     this.$set(this.ticketsData[index], 'identity', this.dataListSelections[index].identity)
      //     this.$set(this.ticketsData[index], 'unit', this.dataListSelections[index].unit)
      //     this.$set(this.ticketsData[index], 'mark', this.dataListSelections[index].userId)
      //     if (this.dataListSelections[index].ticket === '' || this.dataListSelections[index].ticket === undefined || this.dataListSelections[index].ticket === '未分配') {
      //       this.dataListSelections[index].ticket = this.ticket
      //     } else {
      //       var value = 0
      //       for (var i in this.dataListSelections[index].ticket.split(',')) {
      //        if (this.dataListSelections[index].ticket.split(',')[i] !== this.ticket) {
      //          value++
      //        }
      //        if (value === this.dataListSelections[index].ticket.split(',').length) {
      //         this.dataListSelections[index].ticket += ',' + this.ticket
      //        }
      //       }
      //     }
      //     this.finishNumber = 0
      //     this.submitNumber = this.ticketsData.length
      //     if (this.dataListSelections.length - 1 === index - 0) {
      //       this.state = 'list'
      //       this.dataListSelectionDealAgain = []
      //       if (this.ticket === '所有中层干部(校领导票)') {
      //         this.$refs.asynForTool.func('/ticket/schleamiddlecadres/schoolleaderMiddlecadres/list', 'get', this.searchList, true)
      //       } else if (this.ticket === '教学教辅中层干部(同级票)') {
      //         this.$refs.asynForTool.func('/ticket/samelevelteach/samelevelTeach/list', 'get', this.searchList, true)
      //       } else if (this.ticket === '机关中层干部(同级票)') {
      //         this.$refs.asynForTool.func('/ticket/samelevelorgan/samelevelOrgan/list', 'get', this.searchList, true)
      //       } else if (this.ticket === '教辅中层干部(服务对象票)') {
      //         this.$refs.asynForTool.func('/ticket/serviceteach/serviceTeach/list', 'get', this.searchList, true)
      //       } else if (this.ticket === '机关中层干部(服务对象票)') {
      //         this.$refs.asynForTool.func('/ticket/serviceorgan/serviceOrgan/list', 'get', this.searchList, true)
      //       } else if (this.ticket === '中层干部(群众票)') {
      //         this.$refs.asynForTool.func('/ticket/crowdmiddleleader/crowdMiddleleader/list', 'get', this.searchList, true)
      //       }
      //     }
      //   }
      // },
      // 导入成功
      uploadSuccess (res, file) {
        if (res.success) {
          this.$message.success({dangerouslyUseHTMLString: true,
            message: res.msg})
        } else {
          this.$message.error(res.msg)
        }
      },
      // 下载模板
      downloadTpl () {
        this.$utils.download('/peopleinformation/peopleassessdata/peopleAssessInformation/import/template')
      },
      exportExcel () {
        let params = {
          ...this.searchForm
        }
        this.$utils.download('/peopleinformation/peopleassessdata/peopleAssessInformation/export', params)
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      }
    }
  }
</script>
